class Timeslot < ActiveRecord::Base
  belongs_to :instructor
  has_one :cancellation_report
  has_one :driving_lesson
  has_many :registrations, :dependent => :delete_all

  validates :start_time, :presence => true
  validates :end_time, :presence => true
  validates :instructor, :presence => true

  validate :start_time_cannot_be_too_near
  validate :end_cannot_be_before_start
  validate :cannot_overlap
  validate :duration
  validate :time_of_day
  
  def self.build_default
    self.new(
      start_time: default_time_for(Time.now + 24.hours),
      end_time: default_time_for(Time.now + 26.hours)
    )
  end
  
  def self.default_time_for(t)
    minute = ((t.min + 15) / 15) * 15
    hour = t.hour
    if minute >= 60
      minute = 0
      hour += 1
    end 
    Time.new(t.year, t.month, t.day, hour, minute)
  end

  def start_time_cannot_be_too_near
    if start_time < Time.now.advance(:days => 1)
      errors.add(:start_time, "can't be closer than 24 hours")
    end
  end

  def end_cannot_be_before_start
    if end_time <= start_time
      errors.add(:end_time, "can't be before start")
    end
  end

  def cannot_overlap
    timeslots = instructor.timeslots
    timeslots.each do |timeslot|
      if timeslot.end_time > start_time && timeslot.end_time <= end_time
        errors.add(:base, "Can't overlap with other timeslot: #{timeslot.start_time.to_s(:et)} to #{timeslot.end_time.to_s(:et)}")
      elsif timeslot.start_time >= start_time && timeslot.start_time < end_time
        errors.add(:base, "Can't overlap with other timeslot: #{timeslot.start_time.to_s(:et)} to #{timeslot.end_time.to_s(:et)}")
      elsif timeslot.start_time <= start_time && timeslot.end_time >= end_time
        errors.add(:base, "Can't overlap with other timeslot: #{timeslot.start_time.to_s(:et)} to #{timeslot.end_time.to_s(:et)}")
      end
    end
  end

  def duration
    if end_time - start_time > 2.hours
      errors.add(:base, "Can't be over 2 hours long")
    end
  end

  def time_of_day
    if start_time.hour < 8
      errors.add(:start_time, "can't be before 8 AM")
    end
    if end_time.hour > 20 || (end_time.hour == 20 && end_time.min > 0)
      errors.add(:end_time, "can't be after 8 PM")
    end
  end

  def active?
    if start_time > Time.now && cancellation_report.nil?
      return true
    end
    return false
  end

  def unresolved?
    if self.registered? && self.cancellation_report.nil? && self.driving_lesson.nil?
      return true
    end
  end

  def registered?
    !self.active_registration.nil?
  end
  
  def last_registration
    #Registration.where("timeslot_id = ?", self).order("register_time DESC").limit(1).first
    self.registrations.order("register_time DESC").limit(1).first
  end

  def active_registration
    self.registrations.detect {|r| r.cancel_time.nil?}
  end

  def cancel_registration!
    if self.registered?
      self.active_registration.cancel!
    end
  end

  def date
    start_time.to_date.to_s(:rfc822)
  end

  def start_time_string
    start_time.to_s(:time)
  end

  def end_time_string
    end_time.to_s(:time)
  end
end
